var app=getApp();
import{findCanteenNameById} from "../../apis/apis"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canteenId:0,
    commentArr:[],
    canteenArr:["一饭一楼","一饭三楼","二饭","三饭"],
    canteenPickerIndex:0,
    disPicker:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.setNavigationBarTitle({
      title: '悦食生活-评论管理',
    })
    await this.setData({
      canteenId: parseInt(options.canteenId),
      canteenName: findCanteenNameById(options.canteenId, app.globalData.canteenList)
    })
    await this.getAllComment();
    if (app.globalData.userInfo.permission == 2) {
      this.setData({
        disPicker: false,
        canteenPickerIndex: parseInt(options.canteenId) - 1
      })
    } else {
      this.setData({
        canteenPickerIndex: app.globalData.userInfo.permission - 3
      })
    }
    
  },
  canteenPickerChange: function (e) {
    this.setData({
      canteenPickerIndex: e.detail.value,
      canteenName: this.data.canteenArr[this.data.pickerIndex],
      canteenId: parseInt(e.detail.value) + 1
    })
    this.getAllComment();
  },
  getAllComment(){
    wx.request({
      url: app.globalData.apiUrl+'/comment/sthCanteenAllComment',
      data: {
        "token":wx.getStorageSync('token'),
        "canteenID":parseInt(this.data.canteenId)
      },
      method: "POST",
      success: (result) => {
        // console.log(result);
        // console.log(result.data.data.data);
        var newCommentArr=result.data.data.data;
        for (let index = 0; index < newCommentArr.length; index++) {
          newCommentArr[index].commentDate=newCommentArr[index].commentDate.slice(0,10)
          if(newCommentArr[index].isShow==0){
            var newCommentOne={
              commentDate:newCommentArr[index].commentDate,
              commentText:newCommentArr[index].commentText,
              foodName:newCommentArr[index].foodName,
              id:newCommentArr[index].id,
              isShow:newCommentArr[index].isShow,
              nickName:newCommentArr[index].nickName,
              rating:newCommentArr[index].rating,
              showAction:"隐藏中"
            };
            newCommentArr[index]=newCommentOne
          }
          else if(newCommentArr[index].isShow==1){
            var newCommentOne=({
              commentDate:newCommentArr[index].commentDate,
              commentText:newCommentArr[index].commentText,
              foodName:newCommentArr[index].foodName,
              id:newCommentArr[index].id,
              isShow:newCommentArr[index].isShow,
              nickName:newCommentArr[index].nickName,
              rating:newCommentArr[index].rating,
              showAction:"展示中"
            })
            newCommentArr[index]=newCommentOne
          }
        }
        this.setData({
          commentArr:newCommentArr
        })
        // console.log(this.data.commentArr);
      },
      // fail: (err) => {},
      // complete: (res) => {},
    })
  },
  reLoadComment(){
    this.getAllComment();
  }
})